<template>
	<view class="container-conversation">
		
		<!-- 用户的会话列表信息 -->
		<view class="scroll-box" v-if="conversationList.length > 0">
			<view class="uni-list margintop-bar">
				<view v-for="item in conversationList" :key="item.conversationID"
					@tap="handleRoute(item.conversationID)">
					<TUI-conversation-item :data-type="item.type" :conversation="item"></TUI-conversation-item>
				</view>
			</view>
		</view>
		<!-- 用户之前没有跟客服进行聊天，则展示聊天的按钮 -->
		<u-button :custom-style="customStyle" type="success" @click="contact" shape="square" v-else>联系客服</u-button>

	</view>
</template>
<!-- 这里有个加载的补丁逻辑待优化，页面向SDK请求conversationList的时候会有时延，
造成页面的一个抖动，这里加一个if逻辑打一个补丁，后续继续优化 -->
<script>
	import logger from '@/utils/logger';
	import TUIConversationItem from '@/components/tui-conversation/conversation-item/index';
	import TUIMessageList from '@/components/tui-chat/message-list/index';

	export default {
		data() {
			return {
				customStyle: {
					marginTop: '150rpx',
					fontWeight: 'bold',
					height: '100rpx',
					width: '656rpx',
					background: '#5ED1A9'
				},
				conversationList: [],
				showSelectTag: false,
				array: [{
						name: '发起会话'
					},
					{
						name: '发起群聊'
					},
					{
						name: '加入群聊'
					}
				]
			};
		},

		components: {
			TUIConversationItem,
			TUIMessageList
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad() {
			uni.$TUIKit.on(uni.$TUIKitEvent.CONVERSATION_LIST_UPDATED, this.onConversationListUpdated);

			// 判断用户是否登录，如果没有登录，则不能进行聊天
			if (this.isImReady) {
				// 登入后拉去会话列表
				this.getConversationList();
			}
		},
		onShow() {

			if (!this.token) {
				uni.switchTab({
					url: '/pages/tabBar/mine'
				})
				return;
			}
		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {
			uni.$TUIKit.off(uni.$TUIKitEvent.SDK_READY, this.onConversationListUpdated);
		},

		methods: {
			contact() {
				// 打开客服聊天界面(创建一个回话)
				// 客服的userId： dytadmin
				let userID = 'dytadmin';
				uni.navigateTo({
					url: `/imchat/TUI-Chat/chat?conversationID=C2C${userID}`
				});

			},
			handleRoute(id) {
				const url = `/imchat/TUI-Chat/chat?conversationID=${id}`;
				uni.navigateTo({
					url
				});
			},
			onConversationListUpdated(event) {
				logger.log('TUI-conversation | onConversationListUpdated  |ok');
				this.setData({
					conversationList: event.data
				});
			},

			getConversationList() {
				uni.$TUIKit.getConversationList().then(imResponse => {
					logger.log(
						`TUI-conversation | getConversationList | getConversationList-length: ${imResponse.data.conversationList.length}`
					);
					this.setData({
						conversationList: imResponse.data.conversationList
					});
				});
			},

			showMore() {
				this.setData({
					showSelectTag: !this.showSelectTag
				});
			},

			learnMore() {
				uni.navigateTo({
					url: '/imchat/TUI-User-Center/webview/webview?url=https://cloud.tencent.com/product/im'
				});
			},

			handleOnTap(event) {
				this.setData({
						showSelectTag: false
					},
					() => {
						switch (event.currentTarget.dataset.name) {
							case '发起会话':
								this.$createConversation();
								break;

							case '发起群聊':
								this.$createGroup();
								break;

							case '加入群聊':
								this.$joinGroup();

							default:
								break;
						}
					}
				);
			},

			goHomePage() {
				// uni.navigateTo 不能跳转到 tabbar 页面，使用 uni.switchTab 代替
				uni.switchTab({
					url: '/imchat/TUI-Index/index'
				});
			},

			handleEditToggle() {
				this.setData({
					showSelectTag: false
				});
			},

			$createConversation() {
				uni.navigateTo({
					url: '../create-conversation/create'
				});
			},

			$createGroup() {
				uni.navigateTo({
					url: '/imchat/TUI-Group/create-group/create'
				});
			},

			$joinGroup() {
				uni.navigateTo({
					url: '/imchat/TUI-Group/join-group/join'
				});
			}
		}
	};
</script>
<style scoped>
	.container-conversation {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #F7F8FA;
		height: 100vh;
		width: 100%;
	}

	.scroll-box {
		height: 100vh;
		overflow-y: scroll;
	}

	.margintop-bar {
		margin-bottom: 160 rpx;
	}

	.btn-show-more {
		display: flex;
		width: 160rpx;
		height: 160rpx;
		padding-left: 3rpx;

	}

	.picker {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 96rpx;
	}

	.bottom-back {
		position: fixed;
		height: 160rpx;
		width: 100%;
		bottom: 0;
		right: 0;
		left: 0;
		background-color: #F4F5F9;
		z-index: 3;
	}

	.bottom-area {
		flex-direction: column;
		position: absolute;
		bottom: 60rpx;
		right: 0;
		left: 0;
		margin: auto;
		width: 100px;
		display: flex;
		justify-content: center;
		align-items: center
	}

	.im-link {
		width: 218rpx;
		height: 36rpx;
		font-size: 28rpx;
		line-height: 36rpx;
		margin: 0 auto;
		color: #006EFF;
	}

	.conversation-bubble {
		padding-top: 40rpx;
		position: absolute;
		width: 300rpx;
		padding-right: 3px;
		background-color: #FFFFFF;
		height: 320rpx;
		bottom: 220rpx;
		z-index: 100;
		box-shadow: 0 2px 16px 0 rgba(0, 0, 0, 0.08);
		border-radius: 14rpx;
	}

	.conversation-bubble:before,
	.conversation-bubble:after {
		content: "";
		display: block;
		border-width: 20px;
		position: absolute;
		bottom: -40px;
		left: 54px;
		border-style: solid dashed dashed;
		border-color: #fff transparent transparent;
		font-size: 0;
		line-height: 0;
		margin-left: 4px
	}

	.conversation-bubble:after {
		bottom: -33px;
		border-color: #fff transparent transparent;

	}
</style>
